package com.click369.crm.controller;

import java.util.HashMap;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.click369.crm.common.ResultVo;
import com.click369.crm.pojo.CFunction;
import com.click369.crm.service.FunctionService;
import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping("function")
public class FunctionController implements BaseController<CFunction>{
	
	@Autowired
	private FunctionService functionService;
	
	//添加
	@RequiresRoles({"sysadmin"})
	@RequestMapping("save")
	@ResponseBody
	@Override
	public ResultVo save(CFunction f) {
		f.setId(null);
		functionService.save(f);
		return new ResultVo(200,"增加成功");
	}
	
	//根据id删除
	@RequiresRoles({"sysadmin"})
	@RequestMapping("delete")
	@ResponseBody
	@Override
	public ResultVo delete(Integer id) {
		functionService.delete(id);
		return new ResultVo(200,"删除成功");
	}
	
	//修改
	@RequiresRoles({"sysadmin"})
	@RequestMapping("update")
	@ResponseBody
	@Override
	public ResultVo update(CFunction f) {
		Integer id = f.getId();
		if(id == null){
			return new ResultVo(200,"没有id");
		}
		functionService.update(f);
		return new ResultVo(200,"修改成功");
	}
	   
	public ResultVo select(Integer page, Integer rows, CFunction f) {
		PageInfo info = functionService.select(page, rows, f);
		return new ResultVo(info.getTotal(),info.getList());
	}
	
	//根据id查询
	@RequiresRoles({"sysadmin"})
	@RequestMapping("getById")
	@ResponseBody
	@Override
	public ResultVo getById(Integer id) {
		CFunction byid = functionService.getById(id);
		return new ResultVo(byid);
	}
	
	//根据父节点id查询父节点中的子节点
	@RequiresRoles({"sysadmin"})
	@RequestMapping("nodes")
	@ResponseBody
	public ResultVo getByPid(@RequestParam(defaultValue="0")Integer pid) {
		List<HashMap> nodes = functionService.getNodesByPid(pid);
		return new ResultVo(nodes);
	}
	
	@RequiresRoles({"sysadmin"})
	@RequestMapping("getByRId")
	@ResponseBody
	public ResultVo getByRId(@RequestParam(required=true)Integer rid) {
		List<HashMap> nodes = functionService.getNodesByRid(rid);
		return new ResultVo(nodes);
	}
}
